home *** CD-ROM | disk | FTP | other *** search
- /****
- *
- * These are the routines required to actually carry out the document-handling
- * functions. They are invoked from menu_tree() and are arranged in alphabetic
- * order to simplify finding them. Each ensures that the menus are properly
- * enabled and disabled.
- *
- ****/
-
- # include <types.h>
- # include <memory.h>
- # include <packages.h>
- # include <quickdraw.h>
- # include <toolutils.h>
- # include <windows.h>
- # include <dialogs.h>
- # include <menus.h>
- # include <textedit.h>
- # include <string.h>
- # include <files.h>
- # include <resources.h>
-
- # include <iac.h>
- # define PUBLIC extern
- # include <Editor.h>
-
- #define noErr 0 /* 0 for success */
- #define fnfErr -43 /* File not found */
-
- /**
- * Routine: close_doc
- *
- * This is the code required to close (and save if necessary) a document.
- */
-
- # define __SEG__ Main
- short close_doc()
- {
- win_dataH the_data_H; /* data associated with a window */
- extentH the_extH; /* handle to extent block */
- exTable ext_recs; /* ptr to array of extents */
- TEHandle TextH; /* The TextEdit handle */
- short i, item, nm_len;
- short iac_code; /* result from IAC call */
- Str255 st0;
-
- extern short save_doc(); /* saved updated document */
-
- extern WindowPtr myWindow; /* the text window */
-
- # define no 3 /* button id */
-
- the_data_H = (win_dataH) GetWRefCon (myWindow);
- the_extH = (**the_data_H).the_extents;
- TextH = (**the_data_H).wind_TEH;
- ext_recs = *the_extH;
-
- /* check about saving, etc. */
- if ((**the_data_H).dirty)
- {
- nm_len = (**the_data_H).doc_file_nm.length;
- if (nm_len)
- {
- ParamText (&(**the_data_H).doc_file_nm, "", "", "");
- }
- else
- {
- ParamText ("Untitled", "", "", "");
- }
-
- item = StopAlert (SAVE_CHANGES, nil);
- switch (item)
- {
- case ok:
- save_doc();
- break;
-
- case cancel:
- return 0; /* don't delete extents, etc. */
- break;
-
- case no:
- break;
- }
- }
-
- /* shut down extents... */
- for (i=0; i<(**the_data_H).ext_cnt; i++)
- {
- iac_code = iac_remove_dependency(ext_recs[i].src_doc,
- (**the_data_H).the_slot,
- ext_recs[i].hat_check);
-
- if (iac_code != noErr && iac_code != NO_SUCH_DEP)
- {
- NumToString ((long)iac_code, &st0);
- ParamText (&st0,"Remove Dependency",nil,nil);
- item = StopAlert (IAC_ERR_ALRT, nil);
- }
- }
-
- /* clean up storage associated with window */
- the_data_H = (win_dataH) GetWRefCon(myWindow);
- DisposHandle((Handle) (**the_data_H).the_extents);
- DisposHandle((Handle) the_data_H);
- CloseWindow (myWindow);
-
- /* put menu items into proper states */
- EnableItem (MyMenus[fileMenu], newCommand);
- EnableItem (MyMenus[fileMenu], openCommand);
- DisableItem (MyMenus[fileMenu], closeCommand);
- DisableItem (MyMenus[fileMenu], saveCommand);
- DisableItem (MyMenus[editMenu], 0); /* nothing to edit */
- DisableItem (MyMenus[optionsMenu], 0);
- DisableItem (MyMenus[fontMenu], 0);
- DisableItem (MyMenus[sizeMenu], 0);
-
- DeleteMenu (linkdDispMenu); /* no doc, no menu… */
- DisposeMenu (MyMenus[linkdDispMenu]);
- MyMenus[linkdDispMenu] = nil;
-
- DrawMenuBar;
- }
-
-
- /**
- * Routine: create_doc
- *
- * This is the code for creating a new document.
- */
-
- # define __SEG__ Main
- short create_doc()
- {
- win_dataH the_data_H; /* data associated with a window */
-
- extern WindowPtr myWindow; /* the text window */
- extern void setup_wind(); /* set up window */
-
- setup_wind(); /* create window & data structures */
- the_data_H = (win_dataH) GetWRefCon (myWindow);
- (**the_data_H).doc_file_nm.length = 0; /* no name yet */
-
- /* put menu items into proper states */
- DisableItem (MyMenus[fileMenu], newCommand);
- DisableItem (MyMenus[fileMenu], openCommand);
- EnableItem (MyMenus[fileMenu], closeCommand);
- EnableItem (MyMenus[fileMenu], saveCommand);
- EnableItem (MyMenus[editMenu], 0); /* now we have stuff to edit */
- EnableItem (MyMenus[optionsMenu], 0);
- EnableItem (MyMenus[fontMenu], 0);
- EnableItem (MyMenus[sizeMenu], 0);
- DrawMenuBar;
- }
-
-
- /**
- * Routine: open_doc
- *
- * This is the code required to open a document. After opening the doc,
- * reading in the text, and reloading the extents, it checks to see if there
- * are any documents open of which it was a target in a previous life, and
- * re-establishes those links. After that, it re-starts any links for
- * which it was the source.
- */
-
- # define __SEG__ Main
- short open_doc()
- {
- IOParam the_blk;
- Handle txtH; /* Handle to text itself */
- Handle extRH; /* extent-resource handle */
- long int txt_size; /* for TextEdit */
- SFReply a_reply; /* for SFGetFile */
- Point where;
- SFTypeList the_types;
- OSErr an_err;
- short res_refNum; /* for resource work */
- short slot_ID, vers, the_ed; /* for IAC */
- short c_ndx, e_ndx, e_cnt; /* census-walking loops */
- long t_doc; /* temporaries to avoid memory problems */
- short t_slot, t_hatchk;
- exTable ext_recs; /* ptr to extent-array */
- win_dataH the_data_H; /* data associated with a window */
- win_dataH winRH; /* window data from saved file */
- TEHandle TextH; /* The TextEdit handle */
- info_tbl the_census; /* info for each dependency */
-
- short iac_code = noErr; /* result from IAC call */
- short doc_count = 0; /* how many are open? */
- short ext_so_far = 0; /* counter for 'Links' menu */
-
- extern void setup_wind(); /* set up window */
- extern void add_display_cmd(); /* update 'Links' menu */
-
-
- SetPt (&where, 80, 100);
- the_types[0] = (OSType) 0x54455854; /* 'TEXT' */
- SFGetFile (&where, "", nil, 1, &the_types[0], nil, &a_reply.good);
- if (a_reply.good)
- {
- /* an_err = SetVol(nil, &a_reply.vRefNum); /* set default volume */
- an_err = FSOPEN (&a_reply.fName, a_reply.vRefNum, &fRef);
-
- the_blk.ioCompletion = nil;
- the_blk.ioRefNum = fRef;
- an_err = PBGetEOF(&the_blk.qLink, false);
- txt_size = (long int) the_blk.ioMisc;
- txtH = NewHandle (txt_size);
-
- HLock(txtH);
- the_blk.ioCompletion = nil; /* set up driver parameter block */
- the_blk.ioRefNum = fRef;
- the_blk.ioBuffer = *txtH;
- the_blk.ioReqCount = txt_size;
- the_blk.ioPosMode = fsFromStart;
- the_blk.ioPosOffset = 0;
- an_err = PBRead(&the_blk.qLink, false);
-
- setup_wind(); /* set up window and TE record */
- SETWTITLE (myWindow, &a_reply.fName); /* ensure window title OK */
- the_data_H = (win_dataH) GetWRefCon (myWindow);
- TextH = (**the_data_H).wind_TEH;
- TESetText (*txtH, txt_size, TextH); /* put into TE record */
- HUnlock(txtH);
- DisposHandle (txtH); /* no longer needed */
-
- /* get saved data & attach it to the window */
- res_refNum = OPENRESFILE(&a_reply.fName);
- extRH = GetResource ('EXTN', 0); /* get extents */
- DetachResource (extRH); /* remove from resource manager */
- DisposHandle((Handle) (**the_data_H).the_extents); /* kill old table */
- (**the_data_H).the_extents = extRH;
-
- winRH = (win_dataH) GetResource('EXTN', 1); /* get saved window data */
- (**the_data_H).doc_ID = (**winRH).doc_ID;
- (**the_data_H).ext_cnt = (**winRH).ext_cnt;
- DisposHandle((Handle) winRH); /* no longer needed */
-
- the_blk.ioCompletion = nil;
- the_blk.ioRefNum = fRef;
- an_err = PBClose(&the_blk.qLink, false); /* close file */
-
- /* We get a census of existing dependencies, and check them
- * against our extent data. For each match, we make that the
- * "available dependency" and re-establish the link.
- */
- iac_code = iac_census(&doc_count, &the_census);
- if (iac_code == noErr)
- {
- e_cnt = (**the_data_H).ext_cnt;
- HLock(extRH); /* IAC calls may move memory */
-
- for (c_ndx=0; c_ndx<doc_count; c_ndx++) /* walk census */
- {
- ext_recs = *(extentH) extRH; /* pt to base of table */
- for (e_ndx=0; e_ndx<e_cnt; e_ndx++) /* walk extents */
- {
- if ((the_census.ext_entry[c_ndx].doc_ID ==
- ext_recs[e_ndx].src_doc) &&
- (the_census.ext_entry[c_ndx].hat_check ==
- ext_recs[e_ndx].hat_check))
- {
- /* make this dependency "available" for completion */
- iac_code = iac_available_dependency(ext_recs[e_ndx].src_doc,
- ext_recs[e_ndx].hat_check);
-
- /* complete "available dependency" */
- t_doc = 0; /* temps across IAC calls */
- t_hatchk = 0; /* have already made extent avail */
- t_slot = (**the_data_H).the_slot;
- iac_code = iac_complete_dependency(&t_doc,
- &t_slot,
- &t_hatchk);
- (**the_data_H).the_slot = t_slot; /* could change */
-
- /* read initial copy of data from restarted link */
- the_ed = 0;
- iac_code = ext_read(myWindow, &the_ed, e_ndx);
-
- ext_so_far += 1;
- add_display_cmd(e_ndx, ext_so_far); /* add link to menu */
- }
- } /* for */
- } /* got census */
-
- if (e_cnt) /* restart links for which we are the source */
- {
- ext_recs = *(extentH) extRH;
- t_doc = (**the_data_H).doc_ID;
- t_slot = (**the_data_H).the_slot;
- for (e_ndx=0; e_ndx<e_cnt; e_ndx++)
- {
- if (ext_recs[e_ndx].src_doc == (**the_data_H).doc_ID)
- {
- t_hatchk = ext_recs[e_ndx].hat_check;
- the_ed = 0;
- iac_code = iac_add_dependency(&t_doc,
- &t_slot,
- &t_hatchk,
- &the_ed);
- (**the_data_H).the_slot = t_slot; /* could change */
-
- /* write data to restarted dependency */
- iac_code = ext_write(TextH,
- ext_recs[e_ndx].ext_strt,
- /* ext_recs[e_ndx].ext_strt - ext_recs[e_ndx].ext_end, */
- 1, /* temporary splint */
- t_doc,
- t_hatchk,
- &the_ed);
-
- ext_recs[e_ndx].ed_level = the_ed;
- ext_so_far += 1;
- add_display_cmd(e_ndx, ext_so_far); /* add link to menu */
- }
- }
- }
-
- HUnlock(extRH); /* unclutter memory */
- }
-
- /* put menu items into proper states */
- DisableItem (MyMenus[fileMenu], newCommand);
- DisableItem (MyMenus[fileMenu], openCommand);
- EnableItem (MyMenus[fileMenu], closeCommand);
- DisableItem (MyMenus[fileMenu], saveCommand);
- EnableItem (MyMenus[editMenu], 0); /* now we have stuff to edit */
- EnableItem (MyMenus[optionsMenu], 0);
- EnableItem (MyMenus[fontMenu], 0);
- EnableItem (MyMenus[sizeMenu], 0);
- DrawMenuBar;
- }
- }
-
-
- /**
- * Routine: save_doc()
- *
- * This is the code required to save a document.
- */
-
- # define __SEG__ Main
- short save_doc()
-
- {
- win_dataH the_data_H; /* data associated with a window */
- TEHandle TextH; /* The TextEdit handle */
- IOParam the_blk;
- Handle txtH; /* Handle to text itself */
- Handle tmp_H; /* temporary for resource work */
- short nm_len;
- short refNum, res_refNum;
- Str255 prompt,orig_Name;
- SFReply aReply;
- Point where;
- OSErr anErr;
- long int txt_size;
-
- the_data_H = (win_dataH) GetWRefCon (myWindow);
- TextH = (**the_data_H).wind_TEH;
- nm_len = (**the_data_H).doc_file_nm.length;
-
- if (nm_len==0) /* invoke SFPutFile for file name */
- {
- where.h = 80;
- where.v = 80;
- SFPutFile (&where, "Name your new document", "IAC Doc", nil, &aReply);
- if (aReply.good)
- {
- strcpy(&(**the_data_H).doc_file_nm, &aReply.fName); /* CHECK! */
- }
- else
- {
- return 0;
- }
- }
-
- anErr = FSOPEN(&(**the_data_H).doc_file_nm, aReply.vRefNum, &refNum);
- if (anErr) /* may not exist yet.. */
- {
- if (anErr==fnfErr)
- {
- anErr = CREATE(&(**the_data_H).doc_file_nm, aReply.vRefNum, 'IAC1' , 'TEXT');
- anErr = FSOPEN(&(**the_data_H).doc_file_nm, aReply.vRefNum, &refNum);
- (void) CREATERESFILE(&(**the_data_H).doc_file_nm);
- }
- else
- {
- return (anErr);
- }
- }
- res_refNum = OPENRESFILE(&(**the_data_H).doc_file_nm);
-
- /* save text itself in data fork */
- txtH = (**TextH).hText;
- txt_size = GetHandleSize (txtH);
- HLock(txtH);
- the_blk.ioCompletion = nil;
- the_blk.ioRefNum = refNum;
- the_blk.ioBuffer = *txtH;
- the_blk.ioReqCount = txt_size;
- the_blk.ioPosMode = fsFromStart;
- the_blk.ioPosOffset = 0;
- anErr = PBWrite(&the_blk.qLink, false);
- HUnlock(txtH);
-
- /* save extents in 'EXTN' resource */
- if (CountResources ('EXTN')) /* updating existing copy */
- {
- tmp_H = GetResource ('EXTN', 0); /* read old version */
- RmveResource (tmp_H); /* kill it */
- tmp_H = GetResource ('EXTN', 1); /* read old version */
- RmveResource (tmp_H); /* kill it */
- }
- AddResource ((**the_data_H).the_extents, 'EXTN', 0, nil);
- AddResource ((Handle) the_data_H, 'EXTN', 1, nil);
- UpdateResFile (res_refNum);
-
- /* close up shop till next time */
- the_blk.ioCompletion = nil;
- the_blk.ioRefNum = refNum;
- anErr = PBClose(&the_blk.qLink, false); /* close file */
-
- the_blk.ioCompletion = nil;
- the_blk.ioNamePtr = nil;
- the_blk.ioVRefNum = aReply.vRefNum;
- anErr = PBFlushVol (&the_blk.qLink, false); /* ensure disk updated */
-
- SETWTITLE (myWindow, &aReply.fName); /* ensure window title OK */
-
- (**the_data_H).dirty = false;
- return (0);
- }
-
-
- /**
- * Routine: setup_wind
- *
- * This allocates the window and creates the TextEdit data structure and
- * auxiliary data record required.
- */
-
- # define __SEG__ Main
- void setup_wind()
- {
- Rect txRect;
- extentH temp_extH; /* handle to extents block */
- Str255 tStr;
-
- extern WindowPtr myWindow; /* the text window */
- extern WindowRecord wRecord;
- extern short the_fNum, the_size; /* text attributes */
- extern Style the_style;
-
- myWindow = GetNewWindow(windowID, &wRecord, (WindowPtr) -1);
- SetPort(myWindow);
- txRect = myWindow->portRect;
- InsetRect(&txRect, 4, 0);
- TextH = TENew(&txRect, &txRect); /* Not growable, so destRect == viewRect */
- curr_ext_no = -1; /* no 'current extent' yet */
-
- the_data_H = (win_dataH) NewHandle(sizeof(win_data)); /* space for data */
- SetWRefCon (myWindow, (long int) the_data_H); /* keep with window */
- (**the_data_H).the_slot = 0; /* slot_ID for this document */
- (**the_data_H).doc_ID = 0; /* document ID for this doc */
- (**the_data_H).ext_cnt = 0; /* number of extents with this doc */
- (**the_data_H).wind_TEH = TextH; /* TE handle for this window */
- (**the_data_H).dirty = false;
- (**the_data_H).relevent = 0;
-
- /* empty block of extent records */
- temp_extH = (extentH) NewHandle(sizeof(extent));
- (**temp_extH).hat_check = 0;
- (**temp_extH).ed_level = 0;
- (**temp_extH).ext_strt = 0; /* start & end of extent range */
- (**temp_extH).ext_end = 0;
- (**the_data_H).the_extents = temp_extH;
-
- (**TextH).txFont = the_fNum; /* set text attributes */
- (**TextH).txSize = the_size;
- (**TextH).txFace = the_style;
-
- }
-